package com.ruoyi.boat.controller;

import com.ruoyi.boat.aop.AvoidDuplicate;
import com.ruoyi.boat.domain.Msg;
import com.ruoyi.boat.service.IMsgService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息数据Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@Controller
@RequestMapping("/boat/msg")
public class MsgController extends BaseController {
    private String prefix = "boat/msg";

    @Autowired
    private IMsgService msgService;

    @RequiresPermissions("boat:msg:view")
    @GetMapping()
    public String msg() {
        return prefix + "/msg";
    }

    /**
     * 查询消息数据列表
     */
    @RequiresPermissions("boat:msg:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Msg msg) {
        startPage();
        List<Msg> list = msgService.selectMsgList(msg);
        return getDataTable(list);
    }

    /**
     * 导出消息数据列表
     */
    @RequiresPermissions("boat:msg:export")
    @Log(title = "消息数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @AvoidDuplicate
    @ResponseBody
    public AjaxResult export(Msg msg) {
        List<Msg> list = msgService.selectMsgList(msg);
        ExcelUtil<Msg> util = new ExcelUtil<>(Msg.class);
        return util.exportExcel(list, "消息数据");
    }

    /**
     * 新增消息数据
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存消息数据
     */
    @RequiresPermissions("boat:msg:add")
    @Log(title = "消息数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Msg msg) {
        return toAjax(msgService.insertMsg(msg));
    }

    /**
     * 修改消息数据
     */
    @RequiresPermissions("boat:msg:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Msg msg = msgService.selectMsgById(id);
        mmap.put("msg", msg);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息数据
     */
    @RequiresPermissions("boat:msg:edit")
    @Log(title = "消息数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Msg msg) {
        return toAjax(msgService.updateMsg(msg));
    }

    /**
     * 删除消息数据
     */
    @RequiresPermissions("boat:msg:remove")
    @Log(title = "消息数据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(msgService.deleteMsgByIds(ids));
    }
}
